package mapper;

import dao.BaseDao;
import entity.hotel_employee;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HotelEmployeeMapper {
    BaseDao baseDao=new BaseDao();

    public hotel_employee selectByTel(String tel,String password) {
        String sql = "select * from hotel_employee where employee_tel=?";
        Object[] params = {tel};
        ResultSet rs = baseDao.query(sql, params);
        if(rs==null){
            return null;
        }else{
            hotel_employee hotel_employee = null;
            try {
                if(rs.next()){
                    String employee_id = rs.getString("employee_id");
                    String employee_name = rs.getString("employee_name");
                    String employee_sex = rs.getString("employee_sex");
                    String employee_job = rs.getString("employee_job");
                    String employee_tel = rs.getString("employee_tel");
                    String employee_password = rs.getString("employee_password");
                    Date employee_birth = rs.getDate("employee_birth");
                    hotel_employee = new hotel_employee(employee_id,employee_name,employee_sex,employee_job,employee_tel,employee_password,employee_birth);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return hotel_employee;
        }
    }
    public int insert(String tel,String password){
        String sql ="insert into hotel_employee values(hotel_employee_seq.nextval,'待填写','0','普通员工',?,?,'1-1月-1990')";
        Object[] params = {tel, password};
        return baseDao.update(sql,params);
    }

    public List<hotel_employee> selectAll(){
        String sql = "select * from hotel_employee";
        Object[] params = {};
        ResultSet rs = baseDao.query(sql,params);
        if (rs==null){
            return null;
        }else {
            List<hotel_employee> employeeList= new ArrayList<>();
            try {
                while(rs.next()){
                    String employee_id = rs.getString("employee_id");
                    String employee_name = rs.getString("employee_name");
                    String employee_sex = rs.getString("employee_sex");
                    String employee_job = rs.getString("employee_job");
                    String employee_tel = rs.getString("employee_tel");
                    String employee_password = rs.getString("employee_password");
                    Date employee_birth = rs.getDate("employee_birth");
                    hotel_employee hotel_employee = new hotel_employee(employee_id,employee_name,employee_sex,employee_job,employee_tel,employee_password,employee_birth);
                    employeeList.add(hotel_employee);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return employeeList;
        }
    }

    public String selectIdByTel(String tel) {
        String sql = "select employee_id from hotel_employee where employee_tel=?";
        Object[] params = {tel};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            String employee_id = null;
            try {
                if(rs.next()){
                    employee_id = rs.getString("employee_id");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return employee_id;
        }
    }

    public String selectNameByTel(String tel) {
        String sql = "select employee_name from hotel_employee where employee_tel=?";
        Object[] params = {tel};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            String employee_name = null;
            try {
                if(rs.next()){
                    employee_name = rs.getString("employee_name");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return employee_name;
        }
    }

    public hotel_employee selectInfoByTel(String tel) {
        String sql = "select * from hotel_employee where employee_tel=?";
        Object[] params = {tel};
        ResultSet rs = baseDao.query(sql,params);
        if(rs==null){
            return null;
        }else {
            hotel_employee hotel_employee=null;
            try {
                if(rs.next()){
                    String employee_id = rs.getString("employee_id");
                    String employee_name = rs.getString("employee_name");
                    String employee_sex = rs.getString("employee_sex");
                    String employee_job = rs.getString("employee_job");
                    String employee_tel = rs.getString("employee_tel");
                    String employee_password = rs.getString("employee_password");
                    Date employee_birth = rs.getDate("employee_birth");
                    hotel_employee = new hotel_employee(employee_id,employee_name,employee_sex,employee_job
                            ,employee_tel,employee_password,employee_birth);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return hotel_employee;
        }
    }

    public int employeeChangeInfo(hotel_employee hotel_employee) {
        String sql = "update hotel_employee set employee_name=?,employee_sex=?,employee_password=? where employee_tel=?";
        Object[] params = {hotel_employee.getEmployee_name(),hotel_employee.getEmployee_sex()
                ,hotel_employee.getEmployee_password(),hotel_employee.getEmployee_tel()};
        int code = baseDao.update(sql, params);
        return code;
    }


    public int employeeChangeJob(hotel_employee hotel_employee) {
        String sql = "update hotel_employee set employee_job=? where employee_id=?";
        Object[] params = {hotel_employee.getEmployee_job(),hotel_employee.getEmployee_id()};
        return baseDao.update(sql, params);
    }

    public int deleteByTel(String deleteTel) {
        String sql = "delete from hotel_employee where employee_tel=?";
        Object[] params = {deleteTel};
        int code = baseDao.update(sql,params);
        return code;
    }
}
